/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/math.h"

/**
 * @brief Calculates the inverse tangent of two doubles.
 *
 * This function returns the inverse tangent of the given double values.
 * It handles special cases where the input values are 0 or outside the range.
 *
 * @param y The numerator double value.
 * @param x The denominator double value.
 * @return double The inverse tangent of y/x.
 */
double Satan2(double y, double x) {
    if (x == 0) {
        if (y > 0) {
            return DM_PI / 2;
        } else if (y < 0) {
            return -DM_PI / 2;
        } else {
            return DNAN;
        }
    }

    return SAtan2(y, x);
}

/**
 * @brief Calculates the inverse tangent of two doubles using a series expansion.
 *
 * This function returns the inverse tangent of the given double values using a series expansion.
 * It handles special cases where the input values are 0 or outside the range.
 *
 * @param y The numerator double value.
 * @param x The denominator double value.
 * @return double The inverse tangent of y/x.
 */
double SAtan2(double y, double x) {
    if (x > 0) {
        return SAtan(y / x);
    } else if (x < 0 && y >= 0) {
        return SAtan(y / x) + DM_PI;
    } else if (x < 0 && y < 0) {
        return SAtan(y / x) - DM_PI;
    } else if (x == 0 && y > 0) {
        return DM_PI / 2;
    } else if (x == 0 && y < 0) {
        return -DM_PI / 2;
    }

    return DNAN;
}
